//========================================================= 
/**@file com_lc_driver_JniComTransmitHandler.cpp
 * @brief 
 * 
 * @date 2017/6/19   10:28 
 * @author Zhyioo 
 * @version 1.0
 */
//---------------------------------------------------------
#define OPEN_LOGGER
#define NO_INCLUDE_PCSC_SOURCE
#define NO_INCLUDE_USB_SOURCE
#define INCLUDE_ASYNC_SOURCE

#include <zhouyb_lib.h>
#include <zhouyb_src.h>

#include <application/tools/ICmdDriver.h>
using namespace zhou_yb::application::tools;

#include <application/driver/device/JniCmdDriver.h>
using zhou_yb::application::driver::JniHandlerDevice;
//---------------------------------------------------------
typedef JniHandlerDevice<AsyncComDevice> DeviceType;
//---------------------------------------------------------
ObjectPool<DeviceType> _obj_pool;
ObjectPool<LoggerAdapter> _log_pool;
ObjectPool<FolderLogger> _folder_pool;
//---------------------------------------------------------
DeviceType* _GetFD(JniInvoker& jni)
{
    jint fd = 0;
    if(!JniHelper::Get(jni, "_fd", fd))
    {
        return NULL;
    }
    return _obj_pool.InstanceOf(fd);
}
//---------------------------------------------------------
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_lc_driver_JniComTransmitHandler */

#ifndef _Included_com_lc_driver_JniComTransmitHandler
#define _Included_com_lc_driver_JniComTransmitHandler
#ifdef __cplusplus
extern "C" {
#endif

/*
 * Class:     com_lc_driver_JniComTransmitHandler
 * Method:    nativeInit
 * Signature: (Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_com_lc_driver_JniComTransmitHandler_nativeInit
        (JNIEnv *env, jobject jobj, jstring sLogDir)
{
    int fd = _obj_pool.CreateObject();
    if(fd < 1)
        return 0;

    JniConverter cvt(env);
    string logDir = cvt.get_string(sLogDir);
    if(logDir.length() < 1)
        return fd;

    int folderFD = _folder_pool.CreateObject();
    int logFD = _log_pool.CreateObject();

    string title = "ComFD";
    title += ArgConvert::ToString(fd);

    FolderLogger* pFolder = _folder_pool.InstanceOf(folderFD);
    LoggerAdapter* pLog = _log_pool.InstanceOf(logFD);

    pFolder->Open(cvt.get_string(sLogDir).c_str(), title.c_str());
    pLog->Select(*pFolder);

    DeviceType* pDev = _obj_pool.InstanceOf(fd);
    pDev->SelectLogger(*pLog);

    return fd;
}

/*
 * Class:     com_lc_driver_JniComTransmitHandler
 * Method:    Open
 * Signature: (Ljava/lang/String;)Z
 */
JNIEXPORT jboolean JNICALL Java_com_lc_driver_JniComTransmitHandler_Open
        (JNIEnv *env, jobject jobj, jstring devName)
{
    JniInvoker jni(env, jobj);
    DeviceType* pDev = _GetFD(jni);
    if(pDev == NULL)
        return JNI_FALSE;
    JniConverter cvt(env);
    string path = cvt.get_string(devName);
    bool bOK = pDev->Open(path.c_str());
    return bOK ? JNI_TRUE : JNI_FALSE;
}

/*
 * Class:     com_lc_driver_JniComTransmitHandler
 * Method:    SetBaud
 * Signature: (I)Z
 */
JNIEXPORT jboolean JNICALL Java_com_lc_driver_JniComTransmitHandler_SetBaud
        (JNIEnv *env, jobject jobj, jint baud)
{
    JniInvoker jni(env, jobj);
    DeviceType* pDev = _GetFD(jni);
    if(pDev == NULL)
        return JNI_FALSE;
    bool bOK = pDev->SetBaud(baud);
    return bOK ? JNI_TRUE : JNI_FALSE;
}

/*
 * Class:     com_lc_driver_JniComTransmitHandler
 * Method:    IsOpen
 * Signature: ()Z
 */
JNIEXPORT jboolean JNICALL Java_com_lc_driver_JniComTransmitHandler_IsOpen
        (JNIEnv *env, jobject jobj)
{
    JniInvoker jni(env, jobj);
    DeviceType* pDev = _GetFD(jni);
    if(pDev == NULL)
        return JNI_FALSE;
    return pDev->IsOpen() ? JNI_TRUE : JNI_FALSE;
}

/*
 * Class:     com_lc_driver_JniComTransmitHandler
 * Method:    Close
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_lc_driver_JniComTransmitHandler_Close
        (JNIEnv *env, jobject jobj)
{
    JniInvoker jni(env, jobj);
    DeviceType* pDev = _GetFD(jni);
    if(pDev == NULL)
        return;
    pDev->Close();
}

/*
 * Class:     com_lc_driver_JniComTransmitHandler
 * Method:    OnWrite
 * Signature: ([BII)Z
 */
JNIEXPORT jboolean JNICALL Java_com_lc_driver_JniComTransmitHandler_OnWrite
        (JNIEnv *env, jobject jobj, jbyteArray sCmd, jint sLen, jint nTimeoutMs)
{
    JniInvoker jni(env, jobj);
    DeviceType* pDev = _GetFD(jni);
    if(pDev == NULL)
        return JNI_FALSE;
    return pDev->OnWrite(jni, sCmd, sLen, nTimeoutMs);
}

/*
 * Class:     com_lc_driver_JniComTransmitHandler
 * Method:    OnRead
 * Signature: ([B[II)Z
 */
JNIEXPORT jboolean JNICALL Java_com_lc_driver_JniComTransmitHandler_OnRead
        (JNIEnv *env, jobject jobj, jbyteArray rCmd, jintArray rLen, jint nTimeoutMs)
{
    JniInvoker jni(env, jobj);
    DeviceType* pDev = _GetFD(jni);
    if(pDev == NULL)
        return JNI_FALSE;
    return pDev->OnRead(jni, rCmd, rLen, nTimeoutMs);
}

#ifdef __cplusplus
}
#endif
#endif
//---------------------------------------------------------
//========================================================= 
